package com.blog.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blog.entity.Cert;
import com.blog.entity.User;
import com.blog.service.CertService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mars123
 * @since 2019-07-02
 */
@Service
public class CertServiceImpl implements CertService {


    @Override
    public List<Cert> getCertsByUid(Integer id) {
        return new Cert().selectList(new EntityWrapper<Cert>().eq("user_id", id));
    }

    @Override
    public boolean addProduct(HttpSession session, Cert cert) {
        User user = (User) session.getAttribute("user");
        Cert record = new Cert().selectOne(new EntityWrapper<Cert>().eq("user_id", user.getId()).and().eq("product_id", cert.getProductId()));
        if (record != null) {
            record.setNum(record.getNum() + cert.getNum());
            record.updateById();
            session.setAttribute("cert", getCertsByUid(user.getId()));
        } else if (cert.insert()) {
            session.setAttribute("cert", getCertsByUid(user.getId()));
        }
        return false;
    }

    @Override
    public boolean removeProduct(HttpSession session, Integer id) {
        if (new Cert().deleteById(id)) {
            User user = (User) session.getAttribute("user");
            session.setAttribute("cert", getCertsByUid(user.getId()));
        }
        return false;
    }

    @Override
    public Page<Cert> index(Cert param, Page<Cert> page) {
        return null;
    }

    @Override
    public Cert create(Cert model) {
        if (model.insert()) {
            return model;
        }
        return null;
    }

    @Override
    public Cert update(Cert model) {
        return null;
    }

    @Override
    public boolean delete(int id) {
        return new Cert().deleteById(id);
    }

    @Override
    public Cert view(int id) {
        return null;
    }
}
